import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = CommonConfig.tpmUrl + '/v1'
const wmsUrl = CommonConfig.wms + '/v1'
import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// /v1/spare_part/spare_part_list
// 获取备件列表
export function getPartsList ({ category, name, number, pageNum, pageSize }) {
  const newUrl = `${baseUrl}/spare_part/spare_part_list`
  let reqData = {
    params: {
      name,
      number,
      category: category === 'all' ? undefined : category,
      pageNum,
      pageSize
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
export function getPartsListAll ({ category, name, number }) {
  const newUrl = `${baseUrl}/spare_part/spare_part_list_all`
  let reqData = {
    params: {
      name,
      number,
      category: category === 'all' ? undefined : category
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
// DELETE
// /v1/spare_part/spare_part
// 批量删除备件
export function partsDeleteBatch (id) {
  const newUrl = `${baseUrl}/spare_part/spare_part`
  let reqData = {
    params: {
      id
    },
    paramsSerializer: function (id) {
      return qs.stringify(id, qsConfig)
    }
  }
  return axiosInstance.delete(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// ST
// /v1/spare_part/spare_part
// 增加备件
export function setSaveParts (params) {
  const newUrl = baseUrl + '/spare_part/spare_part'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
export function getUpdateParts (params) {
  const newUrl = baseUrl + `/spare_part/spare_part`
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// /v1/spare_part/spare_part_number/{number}
// 根据ID获取备件信息
export function getStockSpareNumber (number) {
  const newUrl = `${baseUrl}/spare_part/spare_part_number/${number}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// /v1/device_spare_part/binding_devices/{id }获取清清单下设备
export function getPareById (id) {
  const newUrl = `${baseUrl}/device_spare_part/binding_devices/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// /v1/mold_standing/minio/download
// 下载模版 - minio
export function downloadToolCode () {
  const newUrl = `${baseUrl}/mold_standing/minio/download`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// POST
// /v1/model_standing/minio/import:batch
// 批量导入 - minio
export function uploadToolCode (query) {
  const newUrl = `${baseUrl}/model_standing/minio/import:batch`
  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}
// GET
// /v1/spare_part/stock_io_list
// 入库单列表
export function getStockListSpare ({ detailType, creatorName, endDate, number, pageNum, pageSize, startDate, status, ioType }) {
  const newUrl = `${wmsUrl}/spare_part/stock_io_list`
  let reqData = {
    params: {
      number,
      ioDateStart: startDate,
      ioDateEnd: endDate,
      ownerId: creatorName,
      ioType,
      status: status === 'all' ? undefined : status,
      detailType: detailType === 'all' ? undefined : detailType,
      pageNum,
      pageSize
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}

// /v1/spare_part/stock_io/{id}
// 删除入库单
export function delStockSpare (id) {
  const newUrl = `${wmsUrl}/spare_part/stock_io/${id}`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

// 创建出入库单
export function addStockSpareSheets ({
  attachments, detailType, ioDate, items, relatedSheetNumber, remark, type, warehouseId, number, status
}) {
  const params = {
    attachments, detailType, ioDate, stockIoItems: items, relatedSheetNumber, remark, ioType: type, warehouseId, number, status
  }
  const newUrl = wmsUrl + '/spare_part/stock_io'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
export function getDevicePartsList ({ name, number, pageNum, pageSize }) {
  const newUrl = `${baseUrl}/device_spare_part/device_spare_part_detail_list`
  let reqData = {
    params: {
      name,
      number,
      pageNum,
      pageSize
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
export function devicePartsDeleteBatch (id) {
  const newUrl = `${baseUrl}/device_spare_part/device_spare_part_detail/${id}`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// 创建设备备件清单
export function setSaveDeviceParts (params) {
  const newUrl = baseUrl + '/device_spare_part/device_spare_part_detail'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 修改设备备件清单
export function updateDeviceParts (params) {
  const newUrl = baseUrl + '/device_spare_part/device_spare_part_detail'
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// 获取设备备件清单
export function getDevicePartById (id) {
  const newUrl = `${baseUrl}/device_spare_part/device_spare_part_detail/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 领用申请
export function getApplyList ({ category, departmentId, number, pageNum, pageSize, status, userId, orderNumber, returnStatus, searchBy, keyword }) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/list`
  let reqData = {
    params: {
      number,
      searchBy,
      keyword,
      returnStatus, // 归还状态：1无法归还2可归还3已归还
      category: category === 'all' ? undefined : category,
      departmentId: departmentId === 'all' ? undefined : departmentId,
      status: status === 'all' ? undefined : status,
      pageNum,
      pageSize,
      orderNumber,
      userId
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
// DELETE
// /v1/spare_part_use_apply/delete/{id}
// 删除领用申请
export function deleteApply (id) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/delete/${id}`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// OST
// /v1/spare_part_use_apply/create
// 创建领用申请
export function saveApply (params) {
  const newUrl = wmsUrl + '/spare_part_use_apply/create'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
//
// /v1/spare_part_use_apply/update
// 更新领用申请
export function updateApply (params) {
  const newUrl = wmsUrl + '/spare_part_use_apply/update'
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

// /v1/spare_part_use_apply/get_one/{id}
// 获取单个
export function getOneApply (id) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/get_one/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/spare_part_use_apply/close/{id}
// 关闭领用申请
export function closeApply (id) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/close/${id}`
  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

// /v1/spare_part_use_apply/receive/{id}
// 接单
export function postReceiveApply (id) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/receive/${id}`
  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/spare_part_use_apply/out_bound
// 出库

export function outBoundApply (params) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/out_bound`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// /v1/order_spare_part/order_bind_spare_part
// 获取工单绑定的备件 ※※※(获取的是保养计划和设备备件清单绑定的备件，用于创建领用申请)
export function getApplyOrder ({ orderId, orderType, pageNum, pageSize, keyword, searchBy, category }) {
  const newUrl = `${baseUrl}/order_spare_part/order_bind_spare_part`
  let reqData = {
    params: {
      pageNum,
      pageSize,
      orderType,
      orderId,
      keyword,
      searchBy,
      category: category === 'all' ? '' : category
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 获取归还备件
export function getItemReturn (id) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/item_return/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// /v1/spare_part_use_apply/item_list获取出库详情
export function getItemList (params) {
  const newUrl = `${wmsUrl}/spare_part_use_apply/item_list`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 获取维修统计
export function getStatisticList ({ dateType, devices
  , dateData, location }) {
  const newUrl = `${baseUrl}/device_repair/statistic`
  let reqData = {
    params: {
      dateType,
      deviceIds: devices[0] === 'all' ? [] : devices,
      endDate: dateData[1],
      level: location.level,
      schemaId: location.id,
      startDate: dateData[0]
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
